package com.shopping.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shopping.dao.impl.BizDaoImpl;
import com.shopping.entity.TbMember;
import com.shopping.service.impl.BizServiceImpl;

public class fg_leftServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public fg_leftServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("gbk");
        response.setCharacterEncoding("gbk");
        response.setContentType("text/html;charset=gbk");

        BizDaoImpl bizdao = new BizDaoImpl();
        BizServiceImpl bsi = new BizServiceImpl();
        bsi.setBizdao(bizdao);

        HttpSession session = request.getSession();

        PrintWriter out = response.getWriter();

        if (session.getAttribute("uesr") == null) {
            // 未登录时用户窗口
            String name = request.getParameter("name");
            String pass = request.getParameter("password");

            TbMember tbmember = new TbMember();

            boolean falg = bsi.tbmember(name, pass);
            String reallyname = bsi.getReallyName(name);

            Date dNow = new Date();
            SimpleDateFormat ft = new SimpleDateFormat("yyyy年MM月dd日E ");
            SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            if (name.equals("")) {
                out.print("<script>");
                out.print("alert('表单信息不能为空!');");
                out.print("window.location.href='index.jsp';");
                out.print("</script>");
                out.flush();
                out.close();
                return;
            }

            if (falg) {
                out.print("<script>");
                // out.print("alert('登陆成功!');");
                out.print("window.location.href='index.jsp';");
                out.print("</script>");
                out.flush();
                out.close();
                session.removeAttribute("uesr");
                session.setAttribute("uesr", 1);
                session.setAttribute("user", name);
                session.setAttribute("date", ft.format(dNow));
                session.setAttribute("date1", date.format(dNow));
                session.setAttribute("reallyname", reallyname);

            } else {
                out.print("<script>");
                out.print("alert('不存在此会员，请重新登录！！！');");
                out.print("window.location.href='index.jsp';");
                out.print("</script>");
                out.flush();
                out.close();
            }
        } else {
            // 登陆后用户窗口
            out.print("<script>");
            out.print("confirm('没有要买的东西了吗？');");
            out.print("window.location.href='index.jsp';");
            out.print("</script>");
            out.flush();
            out.close();
            // request.removeAttribute("TB"); // 删除缓存——会员修改
            session.removeAttribute("uesr");
            session.setAttribute("uesr", null);
        }
    }
}
